package com.lineage.server.model;

import com.lineage.server.model.Instance.L1DoorInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1SkillUse;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.collections.Lists;
import com.lineage.server.world.World;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class L1HauntedHouse {
  public static final int STATUS_NONE = 0;
  
  public static final int STATUS_READY = 1;
  
  public static final int STATUS_PLAYING = 2;
  
  private final List<L1PcInstance> _members = Lists.newArrayList();
  
  private int _hauntedHouseStatus = 0;
  
  private int _winnersCount = 0;
  
  private int _goalCount = 0;
  
  private static L1HauntedHouse _instance;
  
  public static L1HauntedHouse getInstance() {
    if (_instance == null)
      _instance = new L1HauntedHouse(); 
    return _instance;
  }
  
  private void readyHauntedHouse() {
    setHauntedHouseStatus(1);
    L1HauntedHouseReadyTimer hhrTimer = new L1HauntedHouseReadyTimer();
    hhrTimer.begin();
  }
  
  private void startHauntedHouse() {
    setHauntedHouseStatus(2);
    int membersCount = getMembersCount();
    if (membersCount <= 4) {
      setWinnersCount(1);
    } else if (5 >= membersCount && membersCount <= 7) {
      setWinnersCount(2);
    } else if (8 >= membersCount && membersCount <= 10) {
      setWinnersCount(3);
    } 
    L1PcInstance[] membersArray;
    int length = (membersArray = getMembersArray()).length;
    int i = 0;
    while (i < length) {
      L1PcInstance pc = membersArray[i];
      L1SkillUse l1skilluse = new L1SkillUse();
      l1skilluse.handleCommands(pc, 44, pc.getId(), pc.getX(), pc.getY(), 0, 1);
      L1PolyMorph.doPoly((L1Character)pc, 6284, 300, 4);
      i++;
    } 
    Iterator<L1Object> iterator = World.get().getObject().iterator();
    while (iterator.hasNext()) {
      L1Object object = iterator.next();
      if (object instanceof L1DoorInstance) {
        L1DoorInstance door = (L1DoorInstance)object;
        if (door.getMapId() != 5140)
          continue; 
        door.open();
      } 
    } 
  }
  
  public void endHauntedHouse() {
    setHauntedHouseStatus(0);
    setWinnersCount(0);
    setGoalCount(0);
    L1PcInstance[] membersArray;
    int length = (membersArray = getMembersArray()).length;
    int i = 0;
    while (i < length) {
      L1PcInstance pc = membersArray[i];
      if (pc.getMapId() == 5140) {
        L1SkillUse l1skilluse = new L1SkillUse();
        l1skilluse.handleCommands(pc, 44, pc.getId(), pc.getX(), pc.getY(), 0, 1);
        L1Teleport.teleport(pc, 32624, 32813, (short)4, 5, true);
      } 
      i++;
    } 
    clearMembers();
    Iterator<L1Object> iterator = World.get().getObject().iterator();
    while (iterator.hasNext()) {
      L1Object object = iterator.next();
      if (object instanceof L1DoorInstance) {
        L1DoorInstance door = (L1DoorInstance)object;
        if (door.getMapId() != 5140)
          continue; 
        door.close();
      } 
    } 
  }
  
  public void removeRetiredMembers() {
    L1PcInstance[] temp = getMembersArray();
    L1PcInstance[] array;
    int length = (array = temp).length;
    int i = 0;
    while (i < length) {
      L1PcInstance element = array[i];
      if (element.getMapId() != 5140)
        removeMember(element); 
      i++;
    } 
  }
  
  public void sendMessage(int type, String msg) {
    L1PcInstance[] membersArray;
    int length = (membersArray = getMembersArray()).length;
    int i = 0;
    while (i < length) {
      L1PcInstance pc = membersArray[i];
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(type, msg));
      i++;
    } 
  }
  
  public void addMember(L1PcInstance pc) {
    if (!this._members.contains(pc))
      this._members.add(pc); 
    if (getMembersCount() == 1 && getHauntedHouseStatus() == 0)
      readyHauntedHouse(); 
  }
  
  public void removeMember(L1PcInstance pc) {
    this._members.remove(pc);
  }
  
  public void clearMembers() {
    this._members.clear();
  }
  
  public boolean isMember(L1PcInstance pc) {
    return this._members.contains(pc);
  }
  
  public L1PcInstance[] getMembersArray() {
    return this._members.<L1PcInstance>toArray(new L1PcInstance[this._members.size()]);
  }
  
  public int getMembersCount() {
    return this._members.size();
  }
  
  private void setHauntedHouseStatus(int i) {
    this._hauntedHouseStatus = i;
  }
  
  public int getHauntedHouseStatus() {
    return this._hauntedHouseStatus;
  }
  
  private void setWinnersCount(int i) {
    this._winnersCount = i;
  }
  
  public int getWinnersCount() {
    return this._winnersCount;
  }
  
  public void setGoalCount(int i) {
    this._goalCount = i;
  }
  
  public int getGoalCount() {
    return this._goalCount;
  }
  
  public class L1HauntedHouseReadyTimer extends TimerTask {
    public void run() {
      L1HauntedHouse.this.startHauntedHouse();
      L1HauntedHouse.L1HauntedHouseTimer hhTimer = new L1HauntedHouse.L1HauntedHouseTimer();
      hhTimer.begin();
    }
    
    public void begin() {
      Timer timer = new Timer();
      timer.schedule(this, 90000L);
    }
  }
  
  public class L1HauntedHouseTimer extends TimerTask {
    public void run() {
      L1HauntedHouse.this.endHauntedHouse();
      cancel();
    }
    
    public void begin() {
      Timer timer = new Timer();
      timer.schedule(this, 300000L);
    }
  }
}
